<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>取消请求</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
  </head>
  <body>
    <button>发送请求</button>
    <button>取消请求</button>

    <script>
      let btns = document.querySelectorAll('button');

      let cancel
      btns[0].onclick = async function () {
        await axios.post('http://localhost:8081/delay-server/aaa', null, {
            cancelToken: new axios.CancelToken((c) => {cancel = c})
        })
      };

      btns[1].onclick = function () {
        cancel && cancel()
      };
    </script>
  </body>
</html>
